package com.youkeyi.ddy.cloud.application.business.crm.type.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.crm.type.BusinessCustomerTypeCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.type.dto.BusinessCustomerTypeListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.type.dto.BusinessCustomerTypeListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.type.param.BusinessCustomerTypeCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.type.param.BusinessCustomerTypeDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.type.param.BusinessCustomerTypeListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.type.param.BusinessCustomerTypeUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.type.CrmCustomerTypeCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.type.dto.CrmCustomerTypeListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.type.param.CrmCustomerTypeCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.type.param.CrmCustomerTypeDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.type.param.CrmCustomerTypeQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.type.param.CrmCustomerTypeUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * BusinessCustomerTypeController
 *
 * @Author liuxiawang
 * @Date 2024/4/11 4:09 PM
 * @Location shenzhen.china
 */
@Service
public class BusinessCustomerTypeController implements BusinessCustomerTypeCloudService {
    
    @Autowired
    private CrmCustomerTypeCloudService crmCustomerTypeCloudService;

    @Override
    public BusinessCustomerTypeListCloudDto list(BusinessCustomerTypeListCloudParam param) {
        BusinessCustomerTypeListCloudDto dto = new BusinessCustomerTypeListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        CrmCustomerTypeQueryCloudParam customerTypeQueryCloudParam = new CrmCustomerTypeQueryCloudParam();
        customerTypeQueryCloudParam.setCustomerTypePrimaryKeys(param.getCustomerTypePrimaryKeys());
        customerTypeQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        customerTypeQueryCloudParam.setCodes(param.getCodes());
        customerTypeQueryCloudParam.setCodeFuzzy(param.getCodeFuzzy());
        customerTypeQueryCloudParam.setNames(param.getNames());
        customerTypeQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
        customerTypeQueryCloudParam.setLevel(param.getLevel());
        customerTypeQueryCloudParam.setParentKeys(param.getParentKeys());
        customerTypeQueryCloudParam.setPageSize(param.getPageSize());
        customerTypeQueryCloudParam.setCurrentPage(param.getCurrentPage());
        customerTypeQueryCloudParam.setNeedParent(Boolean.TRUE);
        CrmCustomerTypeListCloudDto customerTypeDtos = crmCustomerTypeCloudService.query(customerTypeQueryCloudParam);
        if (null != customerTypeDtos && CollectionUtils.isNotEmpty(customerTypeDtos.getRows())) {
            List<BusinessCustomerTypeListItemCloudDto> items = new ArrayList<>();
            customerTypeDtos.getRows().forEach(customerType -> {
                BusinessCustomerTypeListItemCloudDto item = new BusinessCustomerTypeListItemCloudDto();
                item.setCustomerTypePrimaryKey(customerType.getCustomerTypePrimaryKey());
                item.setName(customerType.getName());
                item.setCode(customerType.getCode());
                item.setLevel(customerType.getLevel());
                item.setParentKey(customerType.getParentKey());
                item.setParentName(customerType.getParentName());
                item.setDesc(customerType.getDesc());
                item.setIsDeleted(customerType.getIsDeleted());
                item.setIsDeletedName(customerType.getIsDeletedName());
                item.setCreateUserKey(customerType.getCreateUserKey());
                item.setCreateTime(customerType.getCreateTime());
                item.setUpdateUserKey(customerType.getUpdateUserKey());
                item.setUpdateTime(customerType.getUpdateTime());
                item.setCreateUserName(customerType.getCreateUserName());
                item.setUpdateUserName(customerType.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(customerTypeDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessCustomerTypeCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getName())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmCustomerTypeCreateCloudParam customerTypeCreateCloudParam = new CrmCustomerTypeCreateCloudParam();
        customerTypeCreateCloudParam.setCorpKey(corpKey);
        customerTypeCreateCloudParam.setName(param.getName());
        customerTypeCreateCloudParam.setCode(param.getCode());
        customerTypeCreateCloudParam.setParentKey(param.getParentKey());
        customerTypeCreateCloudParam.setDesc(param.getDesc());
        customerTypeCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        customerTypeCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return crmCustomerTypeCloudService.create(customerTypeCreateCloudParam);
    }

    @Override
    public ResponseContent update(BusinessCustomerTypeUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getCustomerTypePrimaryKey())
                || StringUtils.isBlank(param.getName())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmCustomerTypeUpdateCloudParam customerTypeUpdateCloudParam = new CrmCustomerTypeUpdateCloudParam();
        customerTypeUpdateCloudParam.setCustomerTypePrimaryKey(param.getCustomerTypePrimaryKey());
        customerTypeUpdateCloudParam.setName(param.getName());
        customerTypeUpdateCloudParam.setCode(param.getCode());
        customerTypeUpdateCloudParam.setParentKey(param.getParentKey());
        customerTypeUpdateCloudParam.setDesc(param.getDesc());
        customerTypeUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        customerTypeUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmCustomerTypeCloudService.update(customerTypeUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(BusinessCustomerTypeDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getCustomerTypePrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmCustomerTypeDeleteCloudParam customerTypeDeleteCloudParam = new CrmCustomerTypeDeleteCloudParam();
        customerTypeDeleteCloudParam.setCustomerTypePrimaryKeys(param.getCustomerTypePrimaryKeys());
        customerTypeDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        customerTypeDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmCustomerTypeCloudService.delete(customerTypeDeleteCloudParam);
    }
}
